package com.example.paralleltaskdispatcher.slice;

import com.example.paralleltaskdispatcher.GlobalTask;
import com.example.paralleltaskdispatcher.MyTask;
import com.example.paralleltaskdispatcher.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.app.dispatcher.Group;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(c -> {
            String dispatcherName = "MyDispatcher";
            // 并发任务分发器
            // dispatcherName: 调度管理器名称
            // TaskPriority.DEFAULT: 优先级
//            TaskDispatcher parallelTaskDispatcher = getContext().createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
//            parallelTaskDispatcher.delayDispatch(new MyTask("task1"), 3000);
//            parallelTaskDispatcher.delayDispatch(new MyTask("task2"), 1000);
//            parallelTaskDispatcher.asyncDispatch(new MyTask("task3"));
            // 创建任务组
//            parallelTaskDispatcher.syncDispatch(new MyTask("task1"));
//            parallelTaskDispatcher.syncDispatch(new MyTask("task2"));
//            parallelTaskDispatcher.syncDispatch(new MyTask("task3"));

//            Group dispatchGroup = parallelTaskDispatcher.createDispatchGroup();
            // 将任务1加入任务组
//            parallelTaskDispatcher.asyncGroupDispatch(dispatchGroup, new MyTask("task1"));
            // 将任务2加入任务组
//            parallelTaskDispatcher.asyncGroupDispatch(dispatchGroup, new MyTask("task2"));
            // 任务3要等任务组中的所有任务执行完后才会执行
//            parallelTaskDispatcher.groupDispatchNotify(dispatchGroup, new MyTask("task3"));

            TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            Group group = globalTaskDispatcher.createDispatchGroup();
            globalTaskDispatcher.asyncGroupDispatch(group, new GlobalTask(123.6, 7533.7));
            globalTaskDispatcher.asyncGroupDispatch(group, new GlobalTask(1513.6, 3833.7));

//            globalTaskDispatcher.syncDispatch(new GlobalTask(123.6, 7533.7));
//            globalTaskDispatcher.syncDispatch(new GlobalTask(1513.6, 3833.7));
//            globalTaskDispatcher.asyncDispatch(new GlobalTask(123.6, 7533.7));
//            globalTaskDispatcher.asyncDispatch(new GlobalTask(1513.6, 3833.7));


        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
